/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.server;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 *
 * @author hamish
 */
public class Props extends Properties {

    public Props() {
        super();
    }

    public Props(Props defaults) {
        super(defaults);
    }

    public ChildProps getChildProps(String prefix) {
        return new ChildProps(this, prefix);
    }

    public Props getParent() {
        return null;
    }

    public boolean hasParent() {
        return false;
    }
    
    public List<String> getList(String key, List<String> defaultValue) {
        final String prop = getProperty(key);
        if (prop == null)
            return defaultValue;
        final String[] tmp = prop.split("\\s*,\\s*");
        final List<String> list = new ArrayList<String>();
        for (String item : tmp) {
            if (item.length() != 0) {
                list.add(item);
            }
        }
        return list;
    }

    public List<String> getList(String key) {
        return getList(key, null);
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        String prop = getProperty(key);
        if (prop == null)
            return defaultValue;
        prop = prop.trim().toLowerCase();
        if (prop.matches("^(yes|true|y|t|1|on)$"))
            return true;
        else if (prop.matches("^(no|false|n|f|0|off)$"))
            return false;
        else return defaultValue;
    }

    public Boolean getBoolean(String key) {
        return getBoolean(key, null);
    }

    public Integer getInteger(String key, Integer defaultValue) {
        String prop = getProperty(key);
        if (prop == null)
            return defaultValue;
        try {
            Integer value = Integer.valueOf(prop);
            return value;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public Integer getInteger(String key) {
        return getInteger(key, null);
    }

    public Long getLong(String key, Long defaultValue) {
        String prop = getProperty(key);
        if (prop == null)
            return defaultValue;
        try {
            Long value = Long.valueOf(prop);
            return value;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public Long getLong(String key) {
        return getLong(key, null);
    }

    public Double getDouble(String key, Double defaultValue) {
        String prop = getProperty(key);
        if (prop == null)
            return defaultValue;
        try {
            Double value = Double.valueOf(prop);
            return value;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public Double getDouble(String key) {
        return getDouble(key, null);
    }

    public static class ChildProps extends Props {

        private final String prefix;
        protected final Props parent;

        public ChildProps(Props parent, String prefix) {
            super(parent);
            this.prefix = prefix;
            this.parent = parent;
        }

        public String getPrefix() {
            return prefix;
        }

        @Override
        public Props getParent() {
            return parent;
        }

        @Override
        public boolean hasParent() {
            return true;
        }

        @Override
        public String getProperty(String key) {
            return super.getProperty(prefix + key);
        }

        @Override
        public String getProperty(String key, String defaultValue) {
            return super.getProperty(prefix + key, defaultValue);
        }

        @Override
        public synchronized Object setProperty(String key, String value) {
            return super.setProperty(prefix + key, value);
        }

//        @Override
//        public Set<String> stringPropertyNames() {
//            super.get
//            final Set<String> longNames = super.stringPropertyNames();
//            final Set<String> shortNames = new HashSet<String>();
//
//            for (String longName : longNames) {
//                if (longName.startsWith(prefix)) {
//                    shortNames.add(longName.substring(prefix.length()));
//                }
//            }
//            return shortNames;
//        }
    }
}
